package com.tfj.mvp.tfj.per;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.tfj.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.tfj.comm.bean.Constant;
import com.tfj.comm.event.LogEvent;
import com.tfj.mvp.base.BaseFragment;
import com.tfj.mvp.tfj.per.bean.AuthMenuBean;
import com.tfj.utils.MyGridLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * Created by HuangQiang on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VPerFragment extends BaseFragment<PPerImpl> implements CPer.IVPer {
    //楼盘负责人权限
    private final AuthMenuBean[] buildAuth = {
            new AuthMenuBean(1, "报备管理", R.mipmap.menu_baobei),
            new AuthMenuBean(2, "成交数据", R.mipmap.menu_deal),
            new AuthMenuBean(3, "扫一扫", R.mipmap.menu_scan),
            new AuthMenuBean(4, "项目消控", R.mipmap.menu_xiaokong),
            new AuthMenuBean(5, "客户管理", R.mipmap.menu_manage),
            new AuthMenuBean(6, "个人信息", R.mipmap.menu_info),
            new AuthMenuBean(7, "刷新置顶", R.mipmap.menu_top),
            new AuthMenuBean(8, "编辑楼盘", R.mipmap.menu_edit),

            new AuthMenuBean(13, "工作总结", R.mipmap.menu_zongjie),
            new AuthMenuBean(14, "请假申请", R.mipmap.menu_qingjia),
            new AuthMenuBean(15, "考勤打卡", R.mipmap.menu_kaoqing),
            new AuthMenuBean(16, "通讯录", R.mipmap.menu_contact),
            new AuthMenuBean(17, "工作提醒", R.mipmap.menu_contact),
    };
    //顾问权限
    private final AuthMenuBean[] adverAuth = {
            new AuthMenuBean(1, "报备管理", R.mipmap.menu_baobei),
            new AuthMenuBean(2, "成交数据", R.mipmap.menu_deal),
            new AuthMenuBean(3, "扫一扫", R.mipmap.menu_scan),
            new AuthMenuBean(4, "项目消控", R.mipmap.menu_xiaokong),
            new AuthMenuBean(6, "个人信息", R.mipmap.menu_info),
            new AuthMenuBean(9, "开启直播", R.mipmap.menu_live),
            new AuthMenuBean(5, "客户管理", R.mipmap.menu_manage),
            new AuthMenuBean(10, "离职", R.mipmap.menu_live),

            new AuthMenuBean(13, "工作总结", R.mipmap.menu_zongjie),
            new AuthMenuBean(14, "请假申请", R.mipmap.menu_qingjia),
            new AuthMenuBean(15, "考勤打卡", R.mipmap.menu_kaoqing),
            new AuthMenuBean(16, "通讯录", R.mipmap.menu_contact),
            new AuthMenuBean(17, "工作提醒", R.mipmap.menu_contact),
    };
    //经纪人权限
    private final AuthMenuBean[] jinAuth = {
            new AuthMenuBean(11, "新房收藏", R.mipmap.menu_star),
            new AuthMenuBean(1, "报备管理", R.mipmap.menu_baobei),
            new AuthMenuBean(2, "成交数据", R.mipmap.menu_deal),
            new AuthMenuBean(12, "查看新房", R.mipmap.menu_check),
            new AuthMenuBean(3, "扫一扫", R.mipmap.menu_scan),
            new AuthMenuBean(6, "个人信息", R.mipmap.menu_info),
            new AuthMenuBean(5, "客户管理", R.mipmap.menu_manage),
            new AuthMenuBean(10, "离职", R.mipmap.menu_live),

            new AuthMenuBean(13, "工作总结", R.mipmap.menu_zongjie),
            new AuthMenuBean(14, "请假申请", R.mipmap.menu_qingjia),
            new AuthMenuBean(15, "考勤打卡", R.mipmap.menu_kaoqing),
            new AuthMenuBean(16, "通讯录", R.mipmap.menu_contact),
            new AuthMenuBean(17, "工作提醒", R.mipmap.menu_contact),
    };
    @BindView(R.id.recyclerView_auth)
    RecyclerView recyclerViewAuth;
    @BindView(R.id.rl_log)
    RelativeLayout rlLog;
    @BindView(R.id.btn_toggle)
    Button btnToggle;
    private boolean ifExpannd = false;

    public static VPerFragment newInstance() {
        VPerFragment fragment = new VPerFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        //登录成功进行监听
        EventBus.getDefault().register(this);
        MyGridLayoutManager myGridLayoutManager = new MyGridLayoutManager(getActivity(), 4);
        myGridLayoutManager.setScrollEnabled(false);
        recyclerViewAuth.setLayoutManager(myGridLayoutManager);
        recyclerViewAuth.setAdapter(quickAdapter_auth);
        List<AuthMenuBean> authMenuBeans = Arrays.asList(jinAuth);
        quickAdapter_auth.replaceData(authMenuBeans.subList(0,8));
        addDisposable(RxView.clicks(rlLog).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {
                    toLogin();
                }));
        addDisposable(RxView.clicks(btnToggle).throttleFirst(0, TimeUnit.SECONDS)
                .subscribe(o -> {
                    btnToggle.setText(ifExpannd?"展开":"收回");
                    Drawable drawable = getResources().getDrawable(ifExpannd?R.mipmap.arrow_up:R.mipmap.arrow_down1);
// 这一步必须要做,否则不会显示.
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    btnToggle.setCompoundDrawables(null, null, drawable, null);
                    quickAdapter_auth.replaceData(ifExpannd?authMenuBeans.subList(0,8):authMenuBeans);
                    quickAdapter_auth.notifyDataSetChanged();
                    ifExpannd = !ifExpannd;

                }));

    }

    //处理登录成功
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dealLog(LogEvent logEvent) {
        if (logEvent.isIfLogin()) {
            //登录
        } else {
            //登出

        }


    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_per;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PPerImpl(mContext, this);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    private QuickAdapter_Auth quickAdapter_auth = new QuickAdapter_Auth();

    public class QuickAdapter_Auth extends BaseQuickAdapter<AuthMenuBean, BaseViewHolder> {
        public QuickAdapter_Auth() {
            super(R.layout.item_authmenu);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final AuthMenuBean item) {
            Button btn_menu = viewHolder.getView(R.id.btn_menu);
            Drawable drawable = getResources().getDrawable(item.getResourceId());
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            btn_menu.setCompoundDrawables(null, drawable, null, null);
            btn_menu.setText(item.getAuthName());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
